package com.carmanage.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.hibernate.Session;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.carmanage.dao.impl.CarshedDaoImpl;
import com.carmanage.domain.Car;
import com.carmanage.domain.Carschedule;
import com.carmanage.domain.Line;
import com.carmanage.util.PageController;

@Controller
public class CarshedController {

	Session session = null;
	@Resource
	private CarshedDaoImpl carshedDaoImpl;

	public CarshedDaoImpl getCarshedDao() {
		return carshedDaoImpl;
	}

	public void setCarshedDao(CarshedDaoImpl carshedDaoImpl) {
		this.carshedDaoImpl = carshedDaoImpl;
	}

	@RequestMapping(value = "/carschedule_add")
	public String save(Carschedule carschedule) {
		System.out.println("hello");
		carshedDaoImpl.saveCarshed(carschedule);
		return "carschedule_all.do";

	}

	@RequestMapping(value = "/carschedule_query")
	public String findCarshcedule(@RequestParam("runs_id") int runs_id,
			@RequestParam("plate") String plate,
			@RequestParam("line_id") int line_id,
			@RequestParam("departure_time") String departure_time,
			HttpServletRequest req) {

		List<Carschedule> carschedules = carshedDaoImpl.findCarschedule(runs_id,
				plate, line_id, departure_time);
		int totalRow = carshedDaoImpl.getTotalRows(runs_id, plate, line_id,
				departure_time);
		PageController pc = new PageController(totalRow, 1);
		req.setAttribute("carsched", carschedules);
		req.setAttribute("pc", pc);
		return "pages/timeManage.jsp";

	}

	@RequestMapping(value = "/carschedule_delete")
	public String deleteDeptById(int schedule_id) {
		System.out.println(schedule_id);
		carshedDaoImpl.deleteCarshedById(schedule_id);
		return "carschedule_all.do";
	}

	@RequestMapping(value = "/carschedule_all")
	public String allCarschedules(HttpSession session) {
		int totalRow = carshedDaoImpl.getTotalRows();
		System.out.println(totalRow);
		PageController pc = new PageController(totalRow, 1);
		List<Carschedule> carsched = carshedDaoImpl.allCarschedules(pc);
		List<Line> lines = carshedDaoImpl.allLineid();
		List<Car> cars = carshedDaoImpl.allPlate();
		session.setAttribute("carsched", carsched);
		session.setAttribute("pc", pc);
		session.setAttribute("lines", lines);
		session.setAttribute("cars", cars);
		return "pages/timeManage.jsp";
	}

	@RequestMapping(value = "/modify")
	public @ResponseBody
	Carschedule modify(@RequestParam("schedule_id") int schedule_id) {
		Carschedule carsched = carshedDaoImpl.findCarshedById(schedule_id);
		System.out.println(carsched);
		return carsched;

	}

	@RequestMapping(value = "/updateCarshed")
	public String changePages(Carschedule carschedule) {
		carshedDaoImpl.updateCarshed(carschedule);
		return "carschedule_all.do";

	}

	@RequestMapping(value = "/page")
	public String changePages(@RequestParam("currentPage") int currentPage,
			HttpSession req) {
		PageController pc = (PageController) req.getAttribute("pc");
		pc.setCurrentPage(currentPage);
		List<Carschedule> carsched = carshedDaoImpl.allCarschedules(pc);
		req.setAttribute("carsched", carsched);
		return "pages/timeManage.jsp";

	}

}
